
#include "cl_base_io_format.h"

// [*] C_LDS_Sparse_SVMLightFormat

void C_LDS_Sparse_SVMLightFormat::myInit(
	string strFN_SrcData_InSVMLightFormat, bool fUseDefaultExampleWeight,
	size_t FEATURE_POOL_SIZE_ScaleMB )
{
	char strTemp[10001];
	sprintf_s(strTemp, "TempData_%d%d%d%d.dat",
		CMyRandom::NextInt(10), CMyRandom::NextInt(10),
		CMyRandom::NextInt(10), CMyRandom::NextInt(10));
	myInit(
		strFN_SrcData_InSVMLightFormat, fUseDefaultExampleWeight,
		strTemp, FEATURE_POOL_SIZE_ScaleMB);
}

void C_LDS_Sparse_SVMLightFormat::myInit(
	string strFN_SrcData_InSVMLightFormat, bool fUseDefaultExampleWeight,
	string strFN_TempDataStorage,
	size_t FEATURE_POOL_SIZE_ScaleMB )
{
	this->strFN_TempDataStorage = strFN_TempDataStorage;

	CDataFileWriter DFW;
	int MaxFeatureDimension = 0;
	DFW.myInit(strFN_TempDataStorage, MaxFeatureDimension);

	char op;
	int lbl, fi, fv;
	CSparseVector sv;
	char strTemp[1000001];

	ifstream inFile(strFN_SrcData_InSVMLightFormat.c_str());
	while (inFile>>lbl)
	{
		if (1) //if (fUseDefaultExampleWeight) //TODO
		{
			sv.Clear();
			inFile.getline(strTemp, 1000000);
			istringstream iss(strTemp);
			while (iss>>fi>>op>>fv)
			{
				if (fi > MaxFeatureDimension) MaxFeatureDimension = fi;
				sv.PushBack(fi, fv);
			}
			DFW.PushBack(sv, lbl, 1.0);
		}
		else
		{
			//TODO
		}
	}
	inFile.close();
	inFile.clear();

	DFW.ModifyFeatureDimension(MaxFeatureDimension + 1);
	DFW.Finish();

	C_LDS_Sparse_ByExample::myInit(
		strFN_TempDataStorage, FEATURE_POOL_SIZE_ScaleMB);
}

C_LDS_Sparse_SVMLightFormat::~C_LDS_Sparse_SVMLightFormat()
{
	C_LDS_Sparse_ByExample::myRelease();

	char strTemp[10001];
	sprintf_s(strTemp, "del %s", strFN_TempDataStorage.c_str());
	CExternalTools::ConsoleTools::RunDosCommand("", strTemp);
}

